<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="发起人" prop="promoter">
        <el-input
          v-model="queryParams.promoter"
          placeholder="请输入发起人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发起时间" prop="promoterTime">
        <el-date-picker
          clearable
          v-model="queryParams.promoterTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择发起时间"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="入库单号" prop="asnreference1">
        <el-input
          v-model="queryParams.asnreference1"
          placeholder="请输入入库单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="供应商" prop="suppliername">
        <el-input
          v-model="queryParams.suppliername"
          placeholder="请输入供应商"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="货主" prop="customerdescr1">
        <el-input
          v-model="queryParams.customerdescr1"
          placeholder="请输入货主"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="SKU" prop="sku">
        <el-input
          v-model="queryParams.sku"
          placeholder="请输入SKU"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="批号" prop="lotatt04">
        <el-input
          v-model="queryParams.lotatt04"
          placeholder="请输入批号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="跟踪号" prop="plantoid">
        <el-input
          v-model="queryParams.plantoid"
          placeholder="请输入跟踪号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="出库单号" prop="orderNo">
        <el-input
          v-model="queryParams.orderNo"
          placeholder="请输入出库单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:except:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:except:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:except:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:except:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="exceptList">
      <!-- <el-table-column label="主键" align="center" prop="id" /> -->
      <el-table-column label="问题描述" align="center" prop="description" />
      <el-table-column label="发起人" align="center" prop="promoter" />
      <el-table-column
        label="发起时间"
        align="center"
        prop="promoterTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.promoterTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="入库单号"
        align="center"
        prop="asnreference1"
        width="150"
      />
      <el-table-column
        label="入库时间"
        align="center"
        prop="dadAddtime"
        width="120"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.dadAddtime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="供应商"
        align="center"
        prop="suppliername"
        width="160"
      />
      <el-table-column label="货主" align="center" prop="customerdescr1" />
      <el-table-column label="审单员" align="center" prop="sdy" />
      <el-table-column label="验收员" align="center" prop="ysy" width="100" />
      <el-table-column label="收货员" align="center" prop="shy" />
      <el-table-column label="上架员" align="center" prop="sjy" />
      <el-table-column label="开票员" align="center" prop="dahHedi01" />
      <el-table-column
        label="批次号"
        align="center"
        prop="lotnum"
        width="100"
      />
      <el-table-column label="SKU" align="center" prop="sku" width="130" />
      <el-table-column
        label="批号"
        align="center"
        prop="lotatt04"
        width="100"
      />
      <el-table-column label="采购数量" align="center" prop="orderedqtyEach" />
      <el-table-column
        label="生产日期"
        align="center"
        prop="lotatt01"
        width="100"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.lotatt01, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="有效期"
        align="center"
        prop="lotatt02"
        width="100"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.lotatt02, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="上架货物"
        align="center"
        prop="tolocation"
        width="120"
      />
      <el-table-column label="上架单位" align="center" prop="touom1" />
      <el-table-column label="金额" align="center" prop="je" />
      <el-table-column label="单价" align="center" prop="dj" />
      <el-table-column label="质量状态" align="center" prop="lotatt08" />
      <el-table-column label="上架件数" align="center" prop="atl2Toqty" />
      <el-table-column label="上架数量" align="center" prop="atl2Toqtyeach" />
      <el-table-column label="跟踪号" align="center" prop="plantoid" />
      <el-table-column label="出库单号" align="center" prop="orderNo" />
      <el-table-column
        fixed="right"
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:except:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:except:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改差异登记对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="500px"
      append-to-body
      :close-on-click-modal="false"
    >
      <div class="dialogCon">
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-form-item label="问题描述" prop="description">
            <el-input
              type="textarea"
              :rows="4"
              v-model="form.description"
              placeholder="请输入问题描述"
            />
          </el-form-item>
          <!-- <el-form-item prop="type">
            <el-radio-group v-model="form.type">
              <el-radio :label="'0'">入库单号</el-radio>
              <el-radio :label="'1'">出库单号</el-radio>
            </el-radio-group>
          </el-form-item> -->
          <el-form-item label="入库单号" prop="asnreference1">
            <el-input
              style="margin-right: 5px"
              v-model="form.asnreference1"
              placeholder="请输入入库单号"
            />
            <el-button type="success" @click="handlePutstorage">查询</el-button>
          </el-form-item>
          <!-- <el-form-item
            label="出库单号"
            prop="orderNo"
            v-show="form.type === '1'"
          >
            <el-input
              style="margin-right: 5px"
              v-model="form.orderNo"
              placeholder="请输入出库单号"
            />
            <el-button type="success">查询</el-button>
          </el-form-item> -->
        </el-form>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog
      :title="title"
      :visible.sync="openPutstorage"
      width="900px"
      append-to-body
      :close-on-click-modal="false"
    >
      <el-table
        v-loading="loading"
        :data="storageList"
        :row-style="rowStyle"
        @row-click="handleRowClick"
        @select="platSelection"
        @select-all="selectAll"
        ref="putTable"
      >
        <!-- <el-table-column type="selection" width="55" align="center" /> -->
        <el-table-column
          label="入库单号"
          align="center"
          prop="asnreference1"
          width="150"
        />
        <el-table-column
          label="入库时间"
          align="center"
          prop="dadAddtime"
          width="120"
        />
        <el-table-column
          label="供应商名称"
          align="center"
          prop="suppliername"
          width="160"
        />
        <el-table-column label="货主" align="center" prop="customerdescr1" />
        <el-table-column label="审单员" align="center" prop="sdy" />
        <el-table-column label="验收员" align="center" prop="ysy" width="100" />
        <el-table-column label="收货员" align="center" prop="shy" />
        <el-table-column label="上架员" align="center" prop="sjy" />
        <el-table-column label="开票员" align="center" prop="dahHedi01" />
        <el-table-column label="SKU" align="center" prop="sku" width="130" />
        <el-table-column
          label="批号"
          align="center"
          prop="lotatt04"
          width="100"
        />
        <el-table-column
          label="批次号"
          align="center"
          prop="lotnum"
          width="100"
        />
        <el-table-column
          label="采购数量"
          align="center"
          prop="orderedqtyEach"
        />
        <el-table-column
          label="生产日期"
          align="center"
          prop="lotatt01"
          width="100"
        />
        <el-table-column
          label="有效期"
          align="center"
          prop="lotatt02"
          width="100"
        />
        <el-table-column
          label="上架货物"
          align="center"
          prop="tolocation"
          width="120"
        />
        <el-table-column label="上架单位" align="center" prop="touom1" />
        <el-table-column label="金额" align="center" prop="je" />
        <el-table-column label="单价" align="center" prop="dj" />
        <el-table-column label="质量状态" align="center" prop="lotatt08" />
        <el-table-column label="上架件数" align="center" prop="atl2Toqty" />
        <el-table-column label="上架数量" align="center" prop="atl2Toqtyeach" />
        <el-table-column label="跟踪号" align="center" prop="plantoid" />
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitPutForm">确 定</el-button>
        <el-button @click="cancelPut">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listExcept,
  getExcept,
  delExcept,
  addExcept,
  updateExcept,
  listputStorage,
} from "@/api/system/except";

export default {
  name: "Except",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 差异登记表格数据
      exceptList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 入库单详情
      openPutstorage: false,
      storageList: [],
      // putTable选中值
      rowData: {},
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        promoter: null,
        promoterTime: null,
        asnreference1: null,
        suppliername: null,
        customerdescr1: null,
        sku: null,
        lotatt04: null,
        plantoid: null,
        orderNo: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        asnreference1: [
          { required: true, message: "入库单号不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    handlePutstorage() {
      // console.log(111, this.form.asnreference1);
      if (this.form.asnreference1 != null) {
        listputStorage({
          orderNo: this.form.asnreference1,
        }).then((res) => {
          this.storageList = res.data;
          this.openPutstorage = true;
        });
      } else {
        this.$message.error("请输入入库单号");
      }
    },
    /** 查询差异登记列表 */
    getList() {
      this.loading = true;
      listExcept(this.queryParams).then((response) => {
        this.exceptList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        description: null,
        promoter: null,
        promoterTime: null,
        asnreference1: null,
        dadAddtime: null,
        suppliername: null,
        customerdescr1: null,
        sdy: null,
        ysy: null,
        shy: null,
        sjy: null,
        dahHedi01: null,
        lotnum: null,
        sku: null,
        lotatt04: null,
        orderedqtyEach: null,
        lotatt01: null,
        lotatt02: null,
        tolocation: null,
        touom1: null,
        je: null,
        dj: null,
        lotatt08: null,
        atl2Toqty: null,
        atl2Toqtyeach: null,
        plantoid: null,
        orderNo: null,
        // type: "0",
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      // this.single = selection.length !== 1;
      // this.multiple = !selection.length;
      this.single = selection.length === 1;
      this.multiple = selection.length === 0;
    },
    handleRowClick(row) {
      this.rowData = row;
      console.log(this.rowData);
    },
    rowStyle({ row, rowIndex }) {
      if (this.rowData === row) {
        return {
          "background-color": "#68ffeb",
          cursor: "pointer",
        };
      }
    },
    submitPutForm() {
      console.log(111, this.rowData);
      if (
        Object.entries(this.rowData).length === 0 &&
        this.rowData.constructor === Object
      ) {
        this.$message({
          type: "warning",
          message: "请选择一条数据!",
        });
      } else {
        this.openPutstorage = false;
      }
    },
    cancelPut() {
      this.openPutstorage = false;
    },
    platSelection(selection, row) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
      // this.platdata = row.id;
      // this.reservationNum = row.platformCode;
      if (this.ids.length > 1) {
        let del_row = selection.shift();
        this.$refs.putTable.toggleRowSelection(del_row, false);
      }
    },
    selectAll() {
      this.$refs.putTable.clearSelection();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加差异登记";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getExcept(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改差异登记";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateExcept(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            const formData = {
              ...this.rowData,
              description: this.form.description,
            };
            // console.log("formData", formData);
            addExcept(formData).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除差异登记编号为"' + ids + '"的数据项？')
        .then(function () {
          return delExcept(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/except/export",
        {
          ...this.queryParams,
        },
        `except_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
<style scoped lang="scss">
.el-input--small {
  width: 220px;
}
.dialogCon {
  .el-date-editor.el-input,
  .el-date-editor.el-input__inner {
    width: 260px;
  }
  .el-input {
    width: 260px;
  }
}
.el-radio__original {
  display: none !important; /* 隐藏原生 radio 输入，但仍然允许交互 */
}
</style>